<?php
/**
 * Created by PhpStorm.
 * User: CY
 * Date: 2018/3/15
 * Time: 9:50
 */

namespace app\api\controller\v1;


use app\api\validate\IDMustBePositiveInt;
use app\lib\exception\MissException;
use think\Controller;
use app\api\model\Category as CategoryModel;

class Category extends Controller
{
    /**
     * 获得全部分类，但不包括分类下的商品
     * @url /category/all
     * @return false|static[]
     * @throws MissException
     */
    public function getAllCategories(){
        $categories = CategoryModel::all([],'img');
        if(empty($categories)){
            throw new MissException([
                'msg' => '还没有任何分类',
                'errorCode' => 50000
            ]);
        }
        return $categories;
    }

    /**
     * 获得具体分类和其对应的商品
     * @url /category/:id
     * @param $id
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws MissException
     */
    public function getCategory($id){
        $validate = new IDMustBePositiveInt();
        $validate->goCheck();
        $category = CategoryModel::getCategory($id);
        if(!$category){
            throw new MissException([
                'msg' => 'category not found'
            ]);
        }
        return $category;
    }
}